package com.seselin.activity;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ListView;

import com.example.library_view_list.R;
import com.seselin.adapter.ViewListAdapter;
import com.seselin.bean.TestBean;
import com.seselin.view.BaseViewPart;
import com.seselin.view.Test1ViewPart;
import com.seselin.view.Test2ViewPart;
import com.seselin.view.Test3ViewPart;

import java.util.ArrayList;

/**
 * Created by Seselin on 2018/9/7 14:43.
 */
public class ViewListActivity extends Activity {

    private ListView listView;
    private ViewListAdapter adapter;
    private ArrayList<View> viewList = new ArrayList<>();
    private ArrayList<BaseViewPart> viewParts = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_list_view);
        listView = findViewById(R.id.list_view);
        initView();
    }

    private void initView() {
        viewList.clear();

        adapter = new ViewListAdapter();
        viewParts.add(new Test1ViewPart(listView));
        viewParts.add(new Test2ViewPart(listView));
        viewParts.add(new Test3ViewPart(listView));
        viewParts.add(new Test2ViewPart(listView));
        viewParts.add(new Test1ViewPart(listView));
        viewParts.add(new Test2ViewPart(listView));
        viewParts.add(new Test1ViewPart(listView));
        viewParts.add(new Test2ViewPart(listView));
        viewParts.add(new Test2ViewPart(listView));
        viewParts.add(new Test2ViewPart(listView));
        viewParts.add(new Test2ViewPart(listView));

        for (BaseViewPart viewPart : viewParts) {
            viewList.add(viewPart.getView());
        }

        adapter.setViewList(viewList);
        listView.setAdapter(adapter);

        listView.post(new Runnable() {
            @Override
            public void run() {
                setData(new TestBean("动态设值"));
            }
        });
    }

    public void setData(Object object) {
        for (BaseViewPart viewPart : viewParts) {
            viewPart.setData(object);
        }
        adapter.notifyDataSetChanged();
    }

}
